<template>
  <div>
    <el-table ref="table_container" header-align="center" :data="data" stripe :height="tableHeight" v-loading="loading" header-cell-class-name="table-header-cell" cell-class-name="table-cell">
      <el-table-column prop="provinceCityAreaName" label="省市区名称" width="150px"/>
      <el-table-column prop="experimentAreaName" label="实验区名称" width="150px"/>
      <el-table-column prop="name" label="学校名称" width="150px" fixed/>
      <el-table-column prop="schoolType" label="学制" width="150px"/>
      <el-table-column prop="headmaster" label="校长姓名" width="150px"/>
      <el-table-column prop="headmasterPhone" label="校长电话" width="150px"/>
      <el-table-column prop="headmasterEmail" label="校长邮箱" width="150px"/>
      <el-table-column prop="contact" label="联系人" width="150px"/>
      <el-table-column prop="contactPhone" label="联系人电话" width="150px"/>
      <el-table-column prop="contactEmail" label="联系人邮箱" width="150px"/>
      <el-table-column prop="isStandout" label="是否示范学校" width="150px"/>
      <el-table-column prop="joinAt" label="加入时间" width="200px"/>
      <el-table-column prop="studentNum" label="学生人数" width="150px"/>
      <el-table-column prop="schoolCode" label="学校编码" width="150px"/>
      <el-table-column prop="code" label="编码" width="150px"/>
      <el-table-column prop="hasApply" label="是否有申请表" width="150px"/>
      <el-table-column prop="homepage" label="学校主页" width="150px"/>
      <el-table-column prop="officialAccounts" label="学校公众号" width="150px"/>
      <el-table-column prop="isExperiment" label="是否实验校授牌" width="150px"/>
      <el-table-column prop="experimentAt" label="实验校授牌时间" width="150px"/>
      <el-table-column prop="standoutAt" label="示范校授牌时间" width="150px"/>
      <el-table-column prop="contactAddress" label="联系地址" width="400px"/>
      <el-table-column align="center" label="操作" width="230px" fixed="right">
        <template slot-scope="scope">
          <el-button class="text-button" type="text" icon="el-icon-document blue" @click="enterVis(scope.$index)">数据可视化</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { GetSchoolTable, OpenVis } from '../../../api/table'
export default {
  name: 'school',
  data () {
    return {
      tableHeight: '0px',
      pagination: {
        pageSize: 10,
        currentPage: 1
      },
      total: 0,
      data: [],
      loading: false
    }
  },
  methods: {
    handleSizeChange (val) {
      this.pagination.pageSize = val
    },
    handleCurrentChange (val) {
      this.pagination.currentPage = val
    },
    flashTable () {
      this.loading = true
      GetSchoolTable(this.pagination.pageSize, this.pagination.currentPage).then((res) => {
        this.total = res.data.total
        this.data = res.data.data
        this.loading = false
      })
    },
    enterVis (index) {
      OpenVis(this.data[index].id)
    },
    reInitTableHeight () {
      if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop < 0) {
        setTimeout(this.reInitTableHeight, 10)
      } else if (document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop <= 500) {
        this.tableHeight = '500px'
      } else {
        this.tableHeight = document.documentElement.clientHeight - 70 - this.$refs['table_container'].$el.offsetTop + 'px'
      }
    }
  },
  mounted () {
    setTimeout(this.reInitTableHeight, 100)
  },
  computed: {
    ...mapGetters([
      'height',
      'provinceId',
      'cityId',
      'areaId',
      'joinYear',
      'schoolName',
      'schoolId',
      'flash'
    ])
  },
  watch: {
    height: function (val) {
      this.$nextTick(this.$nextTick(this.reInitTableHeight))
    },
    'pagination.currentPage': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    'pagination.pageSize': {
      handler () {
        this.flashTable()
      },
      immediate: true,
      deep: true
    },
    provinceId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    cityId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    joinYear: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    areaId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    schoolName: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    schoolId: function (val) {
      this.pagination.currentPage = 1
      this.flashTable()
    },
    flash: function () {
      this.flashTable()
    }
  }
}
</script>

<style scoped>
  @import "../../../styles/common.css";
</style>
